package com.lan.support.page;

import java.util.Map;

public class PageUtils {

    public static StringBuilder page(String tbName, String colSQL) {
        return new StringBuilder("SELECT " + colSQL + " FROM " + tbName + " WHERE is_del=1 ");
    }

    public static StringBuilder page(String tbName) {
        return new StringBuilder("SELECT COUNT(id) FROM " + tbName + " WHERE is_del=1 ");
    }


    public static void page(Paging page, Map<String, Object> param, StringBuilder sb, boolean bool) {
        if (page != null) {
            if (page.getStartTime() != null) {
                param.put("startTime", page.getStartTime());
                if (bool) {
                    sb.append(" WHERE update_time >=:startTime");
                    bool = false;
                } else {
                    sb.append(" AND update_time >= :startTime");
                }
            }

            if (page.getEndTime() != null) {
                param.put("endTime", page.getEndTime());
                if (bool) {
                    sb.append(" WHERE update_time <=:endTime");
                } else {
                    sb.append(" AND update_time <= :endTime");
                }
            }
        }
    }

    public static void page(Paging page, Map<String, Object> param, String column, StringBuilder sb) {
        if (page != null) {
            if (page.getStartTime() != null) {
                param.put("startTime", page.getStartTime());
                sb.append(" AND " + column + " >= :startTime");
            }

            if (page.getEndTime() != null) {
                param.put("endTime", page.getEndTime());
                sb.append(" AND " + column + " <= :endTime");
            }
        }
    }

    public static void page(Paging page, Map<String, Object> param, StringBuilder sb) {
        if (page.getSortOrder() != null) {
            if (page.getSortName() != null) {
                sb.append(" ORDER BY " + page.getSortName());
            } else {
                sb.append(" ORDER BY update_time");
            }
            sb.append(" " + page.getSortOrder());
        }

        sb.append(" LIMIT :pageStart,:pageSize");
        param.put("pageStart", page.getStart());
        param.put("pageSize", page.getPageSize());
    }
}
